package com.elinkway.infinitemovies.ui.activity.commonwebview;


import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.VideoView;

public class CommonWebChromclient extends WebChromeClient implements OnPreparedListener,OnCompletionListener,OnErrorListener{
	
	private WebView webView;
    private TextView mTitleView;

    public CommonWebChromclient(){

    }

    public CommonWebChromclient(TextView mTitleView){
        this.mTitleView = mTitleView;
    }

    @Override
	public void onShowCustomView(View view, CustomViewCallback callback) {
		
		 
        if (view instanceof FrameLayout) 
        { 
            // A video wants to be shown 
            FrameLayout frameLayout = (FrameLayout) view; 
            View focusedChild = frameLayout.getFocusedChild(); 
         
            if (focusedChild instanceof VideoView) 
            { 
                // VideoView (typically API level <11) 
//                VideoView videoView = (VideoView) focusedChild; 
//                // Handle all the required events 
//                videoView.setOnPreparedListener(this); 
//                videoView.setOnCompletionListener(this); 
//                videoView.setOnErrorListener(this); 
            } 
            else // Usually android.webkit.HTML5VideoFullScreen$VideoSurfaceView, sometimes android.webkit.HTML5VideoFullScreen$VideoTextureView 
            { 
                // HTML5VideoFullScreen (typically API level 11+) 
                // Handle HTML5 video ended event 
                if (webView != null && webView.getSettings().getJavaScriptEnabled()) 
                { 
                    // Run javascript code that detects the video end and notifies the interface 
                    String js = "javascript:"; 
                    js += "_ytrp_html5_video = document.getElementsByTagName('video')[0];"; 
                    js += "if (_ytrp_html5_video !== undefined) {"; 
                    { 
                        js += "function _ytrp_html5_video_ended() {"; 
                        { 
                            js += "_ytrp_html5_video.removeEventListener('ended', _ytrp_html5_video_ended);"; 
                            js += "_VideoEnabledWebView.notifyVideoEnd();"; // Must match Javascript interface name and method of VideoEnableWebView 
                        } 
                        js += "}"; 
                        js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);"; 
                    } 
                    js += "}"; 
                    webView.loadUrl(js); 
                } 
            } 
        } 
		
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		return false;
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		
	}

    @Override
    public void onReceivedTitle(WebView view, String title) {
        super.onReceivedTitle(view, title);
        if(mTitleView != null && TextUtils.isEmpty(mTitleView.getText())){
            mTitleView.setText(title);
        }
    }


}
